﻿using RM.BasicLibrary.Models.Databases;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;

namespace RM.BasicLibrary.Models.Repositorys
{
    public interface IRepository : IDisposable
    {
        IDatabase Database { get; set; }
        IRepository Create(IDatabase database = null);
        IRepository Create();
        void SetModels(Action<DbModelBuilder> OnModelCreate);
        #region 事务
        /// <summary>
        /// 开始事务
        /// </summary>
        /// <returns></returns>
        IRepository BeginTrans();
        /// <summary>
        /// 提交
        /// </summary>
        int Commit();
        /// <summary>
        /// 回滚
        /// </summary>
        void Rollback();
        #endregion

        #region insert
        /// <summary>
        /// 插入实体数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        int Insert<T>(T entity) where T : class;
        /// <summary>
        /// 批量插入实体数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="entities">实体数据列表</param>
        /// <returns></returns>
        int Insert<T>(List<T> entities) where T : class;
        #endregion

        #region Update
        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        int Update<T>(T entity) where T : class;
        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="actionFilter">属性过滤</param>
        /// <returns></returns>
        int Update<T>(T entity, Action<DbPropertyEntry> actionFilter) where T : class;
        /// <summary>
        /// 批量更新实体数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="entities">实体数据列表</param>
        /// <param name="actionFilter"></param>
        /// <returns></returns>
        int Update<T>(List<T> entities, Action<DbPropertyEntry> actionFilter = null) where T : class;
        #endregion

        #region delete


        /// <summary>
        /// 删除实体数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="entity">实体数据（需要主键赋值）</param>
        /// <returns></returns>
        int Delete<T>(T entity) where T : class;
        /// <summary>
        /// 批量删除实体数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="entities">实体数据列表</param>
        /// <returns></returns>
        int Delete<T>(List<T> entities) where T : class;
        /// <summary>
        /// 删除表数据（根据Lambda表达式）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="condition"></param>
        /// <returns></returns>
        int Delete<T>(Expression<Func<T, bool>> condition) where T : class, new();
        #endregion

        #region 对象实体 查询
        /// <summary>
        /// 查找一个实体根据主键
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="KeyValue">主键</param>
        /// <returns></returns>
        T Query<T>(object keyValue) where T : class;
        /// <summary>
        /// 查找一个实体（根据表达式）
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="condition">表达式</param>
        /// <returns></returns>
        T Query<T>(Expression<Func<T, bool>> condition) where T : class, new();

        /// <summary>
        /// 获取IQueryable表达式
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <returns></returns>
        IQueryable<T> IQueryable<T>() where T : class, new();
        /// <summary>
        /// 获取IQueryable表达式(根据表达式)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="condition">表达式</param>
        /// <returns></returns>
        IQueryable<T> IQueryable<T>(Expression<Func<T, bool>> condition) where T : class, new();
        /// <summary>
        /// 查询列表（获取表所有数据）
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <returns></returns>
        IEnumerable<T> Querys<T>() where T : class, new();


        /// <summary>
        /// 查询列表(分页)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="pagination">分页数据</param>
        /// <returns></returns>
        IEnumerable<T> Querys<T>(Pagination pagination) where T : class, new();
        /// <summary>
        /// 查询列表(分页)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="condition">表达式</param>
        /// <param name="pagination">分页数据</param>
        /// <returns></returns>
        IEnumerable<T> Querys<T>(Expression<Func<T, bool>> condition, Pagination pagination) where T : class, new();
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="condition">表达式</param>
        /// <returns></returns>
        IEnumerable<T> Querys<T>(Expression<Func<T, bool>> condition) where T : class, new();
#if !NET45
        /// <summary>
        /// 查找一个实体（根据sql）
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="strSql">sql语句</param>
        /// <param name="dbParameter">参数</param>
        /// <returns></returns>
        T Query<T>(string strSql, object dbParameter) where T : class, new();
        /// <summary>
        /// 查询列表(分页)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="strSql">SQL语句</param>
        /// <param name="pagination">分页数据</param>
        /// <returns></returns>
        IEnumerable<T> Querys<T>(string strSql, Pagination pagination) where T : class;
        /// <summary>
        /// 查询列表(分页)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="strSql">SQL语句</param>
        /// <param name="dbParameter">参数</param>
        /// <param name="pagination">分页数据</param>
        /// <returns></returns>
        IEnumerable<T> Querys<T>(string strSql, object dbParameter, Pagination pagination) where T : class;
        /// <summary>
        /// 查询列表根据sql语句(带参数)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="strSql">sql语句</param>
        /// <param name="dbParameter">参数</param>
        /// <returns></returns>
        IEnumerable<T> Querys<T>(string strSql, object dbParameter) where T : class;
        /// <summary>
        /// 查询列表根据sql语句
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="strSql">sql语句</param>
        /// <returns></returns>
        IEnumerable<T> Querys<T>(string strSql) where T : class;

#endif
        #endregion
    }
}

